package com.easyn.CamLinePro;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easyn.CamLinePro.DateTimePickerDialog.OnDateTimeSetListener;
import com.easyn.adapter.RecordSetAdapter;
import com.easyn.command.TimeInfo;
import com.tutk.IOTC.Camera;
import com.tutk.IOTC.IRegisterIOTCListener;

/**
 * 全天候录像列表及设置
 * 
 * @author Kevin
 * 
 *         2015-12-10
 */
public class AllDayRecordActivity extends Activity implements
		IRegisterIOTCListener, OnClickListener {
	private Button btn_add_record_info, btnStartTime, btnStopTime;
	private ArrayList<String> listInfo = null;
	private static TimeInfo mInfo;
	private ListView lissView;
	private Button btnEdit;
	public static boolean mIsEdit = false;
	private RelativeLayout bottombar;
	private Button add_record_info;

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.titlebar);
		btnEdit = (Button) findViewById(R.id.bar_right_btn);
		add_record_info = (Button) findViewById(R.id.add_record_info);
		add_record_info.setVisibility(View.VISIBLE);
		add_record_info.setOnClickListener(this);
		btnEdit.setText(R.string.txt_edit);
		btnEdit.setTextColor(Color.WHITE);
		btnEdit.setVisibility(View.VISIBLE);
		btnEdit.setOnClickListener(btnEditClick);
		TextView tv = (TextView) this.findViewById(R.id.bar_text);
		tv.setText(getText(R.string.title_all_day_record));
		// setTitle(getText(R.string.title_all_day_record));
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_allday_record_set);
		bottombar = (RelativeLayout) findViewById(R.id.gridview_bottom);
		Button btn_delete = (Button) findViewById(R.id.gridview_btn_delete);

		btn_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String[] temp = null;
				if (selectList.size() > 0) {
					temp = new String[selectList.size()];
				}

				for (int i = 0; i < selectList.size(); i++) {

					temp[i] = listInfo.get(selectList.get(i));
				}
				for (int j = 0; j < temp.length; j++) {
					listInfo.remove(temp[j]);
				}

				bottombar.startAnimation(AnimationUtils.loadAnimation(
						AllDayRecordActivity.this, R.anim.bottombar_slide_hide));
				bottombar.setVisibility(View.GONE);
				btnEdit.setText(R.string.txt_edit);
				mIsEdit = false;
				selectList.clear();
				adapter.notifyDataSetChanged();

			}
		});
		listInfo = getIntent().getStringArrayListExtra("listinfo");
		initUI();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getText(R.string.is_sava));
		builder.setPositiveButton(getText(R.string.save),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent();
						intent.putStringArrayListExtra("listinfo", listInfo);
						setResult(5, intent);
						finish();

					}
				});
		builder.setNegativeButton(getText(R.string.cancel),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();

					}
				});
		builder.create().show();

		return super.onKeyDown(keyCode, event);
	}

	private OnClickListener btnEditClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (!mIsEdit) {
				bottombar
						.startAnimation(AnimationUtils.loadAnimation(
								AllDayRecordActivity.this,
								R.anim.bottombar_slide_show));
				bottombar.setVisibility(View.VISIBLE);
				btnEdit.setText(getString(R.string.cancel));
				mIsEdit = true;
				adapter.notifyDataSetChanged();
			} else {
				selectList.clear();
				bottombar
						.startAnimation(AnimationUtils.loadAnimation(
								AllDayRecordActivity.this,
								R.anim.bottombar_slide_hide));
				bottombar.setVisibility(View.GONE);
				btnEdit.setText(R.string.txt_edit);
				mIsEdit = false;
				adapter.notifyDataSetChanged();

			}

		}
	};
	private RecordSetAdapter adapter;
	private ArrayList<Integer> selectList;

	private void initUI() {
		selectList = new ArrayList<Integer>();

		btnStartTime = (Button) findViewById(R.id.start_time);
		btnStartTime.setOnClickListener(this);
		btnStopTime = (Button) findViewById(R.id.stop_time);
		btnStopTime.setOnClickListener(this);
		lissView = (ListView) findViewById(R.id.listView_record_info);
		lissView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 编辑模式
				if (mIsEdit) {

					ImageView itemView = (ImageView) view
							.findViewById(R.id.list_delete);
					if (selectList.contains(position)) {
						selectList.remove(Integer.valueOf(position));
						itemView.setImageResource(R.drawable.delete_befor);
					} else {
						selectList.add(Integer.valueOf(position));
						itemView.setImageResource(R.drawable.delete);
					}
				}
			}
		});
		adapter = new RecordSetAdapter(this, listInfo);
		lissView.setAdapter(adapter);

	}

	public void showDialog(final boolean startOrStop) {
		DateTimePickerDialog dialog = new DateTimePickerDialog(this,
				new TimeInfo(getString(R.string.monday), 0, 0));
		/**
		 * 实现接口
		 */
		dialog.setOnDateTimeSetListener(new OnDateTimeSetListener() {
			public void OnDateTimeSet(AlertDialog dialog, TimeInfo info) {
				AllDayRecordActivity.mInfo = info;
				String temp = "";
				if (info.hour < 10) {
					temp = "0" + info.hour;
				} else {
					temp = "" + info.hour;
				}
				String temp_ = "";
				if (info.mimute < 10) {
					temp_ = "0" + info.mimute;
				} else {
					temp_ = "" + info.mimute;
				}
				// Toast.makeText(
				// AllDayRecordActivity.this,
				// "您输入的日期是：" + temp + ":" + info.mimute
				// + "\t\t\t\t\t\t\t" + info.week,
				// Toast.LENGTH_LONG).show();

				if (startOrStop) {
					btnStartTime.setText(info.week + temp + ":" + temp_);
				} else {
					btnStopTime.setText(info.week + temp + ":" + temp_);
				}
			}
		});
		dialog.show();
	}

	@Override
	public void receiveChannelInfo(Camera arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void receiveFrameData(Camera arg0, int arg1, Bitmap arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void receiveFrameDataForMediaCodec(Camera arg0, int arg1,
			byte[] arg2, int arg3, int arg4, byte[] arg5, boolean arg6, int arg7) {
		// TODO Auto-generated method stub

	}

	@Override
	public void receiveFrameInfo(Camera arg0, int arg1, long arg2, int arg3,
			int arg4, int arg5, int arg6) {
		// TODO Auto-generated method stub

	}

	@Override
	public void receiveIOCtrlData(Camera arg0, int arg1, int arg2, byte[] arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void receiveSessionInfo(Camera arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@SuppressLint("ShowToast")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add_record_info:
			
			String a = (String) btnStartTime.getText().subSequence(0, btnStartTime.length()-5);
			String b = (String) btnStopTime.getText().subSequence(0,  btnStopTime.length()-5);
			String c = (String) btnStartTime.getText();
			String d = (String) btnStopTime.getText();
			String e = (String) btnStartTime.getText().subSequence(btnStartTime.length()-5, btnStartTime.length()-5+2);
			String f = (String) btnStopTime.getText().subSequence(btnStopTime.length()-5, btnStopTime.length()-5+2);
			String g = (String) btnStartTime.getText().subSequence(btnStartTime.length()-5+3, btnStartTime.length());
			String h = (String) btnStopTime.getText().subSequence(btnStopTime.length()-5+3, btnStopTime.length());
			if (!a.equals(b)) {
				Toast.makeText(AllDayRecordActivity.this, getString(R.string.same_day), 0)
						.show();

				return;
			}
			if (c.equals(d) || Integer.parseInt(e) > Integer.parseInt(f)) {
				Toast.makeText(AllDayRecordActivity.this,getString(R.string.later_than_start_time), 0)
						.show();

				return;
			}
			if (Integer.parseInt(e) == Integer.parseInt(f)
					&& Integer.parseInt(g) > Integer.parseInt(h)) {
				Toast.makeText(AllDayRecordActivity.this, getString(R.string.later_than_start_time), 0)
						.show();
				return;
			}
			String info = btnStartTime.getText()
					+ "——"
					+ btnStopTime.getText().subSequence(btnStopTime.length()-5,
							btnStopTime.getText().length());
			if (listInfo.contains(info)) {
				Toast.makeText(AllDayRecordActivity.this,
						getString(R.string.is_added), 0).show();
			} else {
				listInfo.add(info);
				adapter.notifyDataSetChanged();
			}

			break;
		case R.id.start_time:
			showDialog(true);
			break;
		case R.id.stop_time:
			showDialog(false);
			break;
		default:
			break;
		}

	}
}
